package com.example.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.entity.mes.SkedEquipment;
import com.example.service.SkedEquipmentService;
import com.example.mapper.SkedEquipmentMapper;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author 14501
* @description 针对表【sked_equipment】的数据库操作Service实现
* @createDate 2024-03-14 15:07:36
*/
@Service
@Log
public class SkedEquipmentServiceImpl extends ServiceImpl<SkedEquipmentMapper, SkedEquipment> implements SkedEquipmentService{

    @Resource
    private SkedEquipmentMapper skedEquipmentMapper;

    @Override
    @DS("mes")
    @Transactional(rollbackFor = Exception.class) // 批量执行需要加上事务,其中报错,进行回滚操作
    public Result insertskedEquipmentBatch(List<SkedEquipment> skedEquipment) {
      try {
          List<String> list = new ArrayList<>();
          Integer integer = 0;
          for (SkedEquipment arr:skedEquipment) {
              SkedEquipment eid = this.skedEquipmentMapper.selectOne(new QueryWrapper<SkedEquipment>().eq("eid", arr.getEid()));
              log.info("query SkedEquipment eid=arr.getEid { }");
              if (eid==null){
                  int insert = this.skedEquipmentMapper.insert(arr);
                  if (insert>0){
                      integer++;
                  }
              }else{
                  list.add("设备已添加");
              }
          }
         if (integer!=skedEquipment.size()){
             return Result.error(list, "新增异常");
         }
          return Result.success("新增成功");
      }catch (Exception e){
          return Result.error("新增异常"+e);
      }
    }

    @Override
    @DS("mes")
    @Transactional(rollbackFor = Exception.class) // 批量执行需要加上事务,其中报错,进行回滚操作
    public Result deleteSkedEqBatch(List<Integer> idlist) {
        try{
            boolean b = this.removeByIds(idlist);
            if (b){
                return Result.success("删除成功");
            }else{
                return Result.error("删除失败","500");
            }
        }catch (Exception e){
            return Result.error("删除发生异常"+e,"500");
        }
    }
}




